package definite.Guide.Book.Menu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.util.Random;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MenuSample {

	private static JTextArea textArea;
	private static Thread thread;
	private static Timer t;
	private static Runnable r;

	static class MenuActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			textArea.append("Selected :  " + e.getActionCommand() + "\n");
			// System.out.println("Selected :  " + e.getActionCommand());
		}

	}

	public static void start(Thread r) {
		r.start();
	}

	public static void stop(Thread r) {
		r.stop();
	}

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			private JMenuBar menuBar = new JMenuBar();
			private JPanel panel = new JPanel();
			private JPanel optionPane = new JPanel();

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("JMenu Demo");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				ActionListener menuActionListener = new MenuActionListener();

				// JMenu FILE
				JMenu File = new JMenu("File");
				File.setMnemonic(KeyEvent.VK_F);
				menuBar.add(File);

				// File -> New
				JMenuItem New = new JMenuItem("New");
				New.setMnemonic(KeyEvent.VK_N);
				New.addActionListener(menuActionListener);
				File.add(New);

				// File -> Open
				JMenuItem Open = new JMenuItem("Open");
				Open.setMnemonic(KeyEvent.VK_O);
				Open.addActionListener(menuActionListener);
				File.add(Open);

				// File -> Close
				JMenuItem Close = new JMenuItem("Close");
				Close.setMnemonic(KeyEvent.VK_C);
				Close.addActionListener(menuActionListener);
				File.add(Close);

				// Separator
				File.addSeparator();

				// File -> Save
				JMenuItem Save = new JMenuItem("Save");
				Save.setMnemonic(KeyEvent.VK_S);
				Save.addActionListener(menuActionListener);
				File.add(Save);

				File.addSeparator();

				// File ->Exit
				JMenuItem Exit = new JMenuItem("Exit");
				Exit.setMnemonic(KeyEvent.VK_X);
				Exit.addActionListener(menuActionListener);
				File.add(Exit);

				// JMenu EDIT
				JMenu Edit = new JMenu("Edit");
				Edit.setMnemonic(KeyEvent.VK_E);
				menuBar.add(Edit);

				// Edit -> Cut
				JMenuItem Cut = new JMenuItem("Cut");
				Cut.setMnemonic(KeyEvent.VK_T);
				Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
						ActionEvent.CTRL_MASK));
				Cut.addActionListener(menuActionListener);
				Edit.add(Cut);

				// Edit -> Copy
				JMenuItem Copy = new JMenuItem("Copy");
				Copy.setMnemonic(KeyEvent.VK_C);
				Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
						ActionEvent.ALT_MASK));
				Copy.addActionListener(menuActionListener);
				Edit.add(Copy);

				// Edit -> Paste
				JMenuItem Paste = new JMenuItem("Paste");
				Paste.setMnemonic(KeyEvent.VK_P);
				Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
						ActionEvent.ALT_MASK));
				Paste.addActionListener(menuActionListener);
				Edit.add(Paste);

				Edit.addSeparator();

				// Edit -> Find
				JMenuItem Find = new JMenuItem("Find");
				Find.setMnemonic(KeyEvent.VK_F);
				Find.setAccelerator(KeyStroke.getKeyStroke("F3"));
				Find.addActionListener(menuActionListener);
				Edit.add(Find);

				// Edit -> Option Submenu
				JMenu Option = new JMenu("Option");
				Option.setMnemonic(KeyEvent.VK_0);
				Icon imageIcon = new ImageIcon("right.gif");
				Option.setIcon(imageIcon);

				// Edit -> Option -> Forward
				ButtonGroup directGroup = new ButtonGroup();

				JCheckBoxMenuItem forward = new JCheckBoxMenuItem("Forward");
				forward.setSelected(true);
				forward.setMnemonic(KeyEvent.VK_F);
				forward.addActionListener(menuActionListener);
				directGroup.add(forward);
				Option.add(forward);

				// Edit -> Option -> BackWard
				JCheckBoxMenuItem backward = new JCheckBoxMenuItem("Backward");
				backward.setMnemonic(KeyEvent.VK_B);
				backward.addActionListener(menuActionListener);
				directGroup.add(backward);
				Option.add(backward);

				// add component
				JMenuItem fileAbout = new JMenuItem("About");
				Action showAction = new ShowAction(fileAbout);
				fileAbout.addActionListener(showAction);
				File.addSeparator();
				File.add(fileAbout);

				JMenuItem editAbout = new JMenuItem("About");
				showAction = new ShowAction(editAbout);
				Edit.addSeparator();
				Edit.add(editAbout);

				Edit.add(Option);

				menuBar.add(Edit);
				Random random = new Random();
				menuBar.setBackground(new Color(random.nextFloat(), random
						.nextFloat(), random.nextFloat()));

				// format the textArea
				textArea = new JTextArea();
				textArea.setEditable(false);
				textArea.setLineWrap(true);
				textArea.setWrapStyleWord(true);

				JScrollPane crollPane = new JScrollPane(textArea);
				crollPane
						.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
				crollPane
						.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

				TitledBorder border = BorderFactory
						.createTitledBorder(null, "Action Results",
								TitledBorder.CENTER, TitledBorder.TOP);
				panel.setBorder(border);
				panel.setLayout(new GridLayout(1, 1));
				panel.add(crollPane);

				// ************************************************************
				// Button to set Color
				final JButton btnColor = new JButton("Random Color");
				btnColor.setMnemonic(KeyEvent.VK_D);
				final Random rdColor = new Random();
				ActionListener listener = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						Color color = new Color(rdColor.nextFloat(),
								rdColor.nextFloat(), rdColor.nextFloat());
						textArea.setBackground(color);
						menuBar.setBackground(new Color(
								1 - rdColor.nextFloat(), 1 - rdColor
										.nextFloat(), 1 - rdColor.nextFloat()));
					}
				};

				ChangeListener changeListener = new ChangeListener() {

					@Override
					public void stateChanged(ChangeEvent e) {
						Random random = new Random();
						float r, g, b;
						AbstractButton button = (AbstractButton) e.getSource();
						ButtonModel model = button.getModel();
						boolean isPress = model.isPressed();
						boolean isArm = model.isArmed();
						boolean isSelected = model.isSelected();
						if (isPress | isArm | isSelected) {
							r = random.nextFloat();
							b = random.nextFloat();
							g = random.nextFloat();
							Color color1 = new Color(r, g, b);
							Color color2 = new Color(1 - r, 1 - g, 1 - b);
							textArea.setBackground(color1);
							menuBar.setBackground(color2);
						}
					}
				};

				// end of Action

				btnColor.addActionListener(listener);
				btnColor.addChangeListener(changeListener);
				// *****************************************END BUTTON

				// COLOR*********************************
				TitledBorder border3 = new TitledBorder(null,
						"Customize Components Color", TitledBorder.CENTER,
						TitledBorder.TOP, new Font("Serif", Font.BOLD, 16),
						Color.RED);
				optionPane.setBorder(border3);
				optionPane.setLayout(new GridLayout(1, 0));
				optionPane.add(btnColor);

				// *** AUTO CHANGE COLOR IN SECS

				ActionListener loopAction = new ActionListener() {
					Random random = new Random();
					Random random1 = new Random();
					Random random2 = new Random();

					float r, g, b;
					float r1, g1, b1;
					float r2, g2, b2;

					@Override
					public void actionPerformed(ActionEvent e) {
						r = random.nextFloat();
						g = random.nextFloat();
						b = random.nextFloat();
						r1 = random1.nextFloat();
						g1 = random1.nextFloat();
						b1 = random1.nextFloat();

						r2 = random2.nextFloat();
						g2 = random2.nextFloat();
						b2 = random2.nextFloat();

						Color color1 = new Color(r, g, b);
						Color color2 = new Color(1 - r, 1 - g, 1 - b);
						Color color3 = new Color(r1, g1, b1);
						Color color4 = new Color(r2, g2, b2);
						Color color5 = new Color(1 - r2, 1 - g2, 1 - b2);

						optionPane.setBackground(color4);
						textArea.setBackground(color1);
						menuBar.setBackground(color2);
						panel.setBackground(color3);
					}
				};

				t = new Timer(0, loopAction);
				r = new Runnable() {

					@Override
					public void run() {
						t.start();
					}
				};

				final JCheckBox auto = new JCheckBox("Auto");
				auto.setSelected(false);

				ItemListener check = new ItemListener() {

					@Override
					public void itemStateChanged(ItemEvent e) {
						final Thread thred = new Thread(r);
						int state = e.getStateChange();
						if (state == ItemEvent.SELECTED)
							thred.start();
						if (state == ItemEvent.DESELECTED)
							t.stop();

					}
				};

				auto.addItemListener(check);
				optionPane.add(auto);
				// ***** END AUTO COLOR

				frame.setSize(350, 250);
				frame.setJMenuBar(menuBar);
				frame.add(panel, BorderLayout.CENTER);
				frame.add(optionPane, BorderLayout.SOUTH);
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(runner);
	}
}
